home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / grafica / grafica2 / mandel.lha / mandel.c < prev    next >
C/C++ Source or Header  |  1996-12-06  |  5KB  |  301 lines

  1. #include <stdlib.h>
  2. #include <setjmp.h>
  3. #include <intuition/intuition.h>
  4. #include <intuition/screens.h>
  5. #include <proto/intuition.h>
  6. #include <proto/graphics.h>
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9.  
  10. #define width 320
  11. #define height 256
  12. #define depth 5
  13.  
  14.  
  15. #define double float
  16.  
  17. struct IntuitionBase * IntuitionBase=0;
  18. struct GfxBase * GfxBase=0;
  19. struct Screen * scrn=0;
  20. struct Window * win=0;
  21. struct RastPort * rp;
  22. struct MsgPort * mp;
  23. struct IntuiMessage * msg;
  24. void Cleanup(void);
  25. void HandleMsg(void);
  26. ULONG secs,msecs,olds=0,oldms=0;
  27. void zoomin(WORD,WORD);
  28. void zoomout(WORD,WORD);
  29. void drawmandel(void);
  30. double x1,x2,y1,y2;
  31. int maxcount;
  32. jmp_buf jb;
  33. int oldpri=0;
  34.  
  35.  
  36. UBYTE ct[][3]=
  37.     {
  38.     { 11, 2, 2 },
  39.     { 11, 11, 11},
  40.     { 0, 10, 2 },
  41.     { 8, 8, 8 },
  42.     { 2, 2, 12 },
  43.     { 13, 13, 13 },
  44.     { 11, 0, 12 },
  45.     { 11, 11, 1 },
  46.     { 2, 10, 12 },
  47.     { 1, 5, 14 },
  48.     { 7, 8, 10 },
  49.     { 14, 14, 13 }
  50.     };
  51.  
  52.  
  53.  
  54.  
  55. int main(int argc,char *argv[])
  56.     {
  57.     int i;
  58.     
  59.     if(argc!=6 && argc!=1)
  60.         {
  61.         Printf("%s: [x1 x2 y1 y2 maxcount]\n",argv[0]);
  62.         exit(EXIT_FAILURE);
  63.         }
  64.         
  65.     if(argc==6)    
  66.         {
  67.         x1=atof(argv[1]);
  68.         x2=atof(argv[2]);    
  69.         y1=atof(argv[3]);
  70.         y2=atof(argv[4]);
  71.         maxcount=atoi(argv[5]);
  72.         }
  73.     else    /* no arguments given */
  74.         {
  75.         x1=-2.2;
  76.         x2=1.1;
  77.         y1=-1.4;
  78.         y2=1.4;
  79.         maxcount=50;
  80.         }
  81.         
  82.         
  83.         
  84.         
  85.     if(atexit(Cleanup)) return EXIT_FAILURE;
  86.     if(!(IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",37))) exit(EXIT_FAILURE);
  87.     if(!(GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",37))) exit(EXIT_FAILURE);
  88.     
  89.     scrn=OpenScreenTags(NULL,
  90.                         SA_Width,width,
  91.                         SA_Height,height,
  92.                         SA_Depth,depth,
  93.                         SA_ShowTitle,FALSE,
  94.                         SA_Type,CUSTOMSCREEN,
  95.             /*            SA_DisplayID,HIRES_KEY, */
  96.                         TAG_DONE);
  97.     if(!scrn) exit(EXIT_FAILURE);
  98.     win=OpenWindowTags(NULL,
  99.                         WA_Left,0,
  100.                         WA_Top,0,
  101.                         WA_Width,width,
  102.                         WA_Height,height,
  103.                         WA_CustomScreen,(ULONG) scrn,
  104.                         WA_Borderless,TRUE,
  105.                         WA_Backdrop,TRUE,
  106.                         WA_Activate,TRUE,
  107.                         WA_SmartRefresh,TRUE,
  108.                         WA_NoCareRefresh,TRUE,
  109.                         WA_RMBTrap,TRUE,
  110.                         WA_IDCMP,IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS,
  111.                         TAG_DONE);
  112.     if(!win) exit(EXIT_FAILURE);
  113.     
  114.     
  115.     rp=win->RPort;
  116.     mp=win->UserPort;
  117.     oldpri=SetTaskPri(FindTask(0),-1);
  118.     
  119.     for(i=0;i<=11;i++)
  120.         {
  121.         SetRGB4(&(scrn->ViewPort),i+20,ct[i][0],ct[i][1],ct[i][2]);
  122.         }
  123.     
  124.     setjmp(jb);
  125.     SetRast(rp,0);
  126.     drawmandel();
  127.     
  128.     
  129.     while(1)
  130.         {
  131.         WaitPort(mp);
  132.         HandleMsg();
  133.         }
  134.     
  135.     
  136.     
  137.     return 0;
  138.     }
  139.     
  140.     
  141.     
  142.     
  143. void Cleanup(void)    
  144.     {
  145.     if(win!=NULL) CloseWindow(win);
  146.     if(scrn!=NULL) CloseScreen(scrn);
  147.     if(GfxBase!=NULL) CloseLibrary((struct Library *)GfxBase);
  148.     if(IntuitionBase!=NULL) CloseLibrary((struct Library *)IntuitionBase);
  149.     SetTaskPri(FindTask(NULL),oldpri);
  150.     }
  151.     
  152.     
  153. void HandleMsg(void)
  154.     {
  155.     if(msg=(struct IntuiMessage *)GetMsg(mp))
  156.         {
  157.         switch(msg->Class)
  158.             {
  159.             case IDCMP_MOUSEBUTTONS:
  160.                 if((msg->Code) == SELECTDOWN)            
  161.                     {
  162.                     secs=msg->Seconds;    
  163.                     msecs=msg->Micros;
  164.                     if(DoubleClick(olds,oldms,secs,msecs))
  165.                         {
  166.                         olds=secs;    
  167.                         oldms=msecs;
  168.                         zoomin(msg->MouseX,msg->MouseY);
  169.                         ReplyMsg((struct Message *)msg);
  170.                         longjmp(jb,1);
  171.                         }
  172.                     else
  173.                         {
  174.                         olds=secs;
  175.                         oldms=msecs;
  176.                         }
  177.                     }
  178.                 else if((msg->Code) == MENUDOWN )
  179.                     {
  180.                     secs=msg->Seconds;    
  181.                     msecs=msg->Micros;
  182.                     if(DoubleClick(olds,oldms,secs,msecs))
  183.                         {
  184.                         olds=secs;    
  185.                         oldms=msecs;
  186.                         zoomout(msg->MouseX,msg->MouseY);
  187.                         ReplyMsg((struct Message *)msg);
  188.                         longjmp(jb,1);
  189.                         }
  190.                     else
  191.                         {
  192.                         olds=secs;
  193.                         oldms=msecs;
  194.                         }
  195.                     }
  196.                 break;
  197.             case IDCMP_VANILLAKEY:
  198.                 switch(msg->Code)
  199.                     {    
  200.                     case 'q':
  201.                     case 27:  /* Escape key */
  202.                         ReplyMsg((struct Message *)msg);
  203.                         exit(0);
  204.                     case 'z':
  205.                         zoomin(width/2,height/2);
  206.                         ReplyMsg((struct Message *)msg);
  207.                         longjmp(jb,1);
  208.                         exit(EXIT_FAILURE); /* should never reach here */
  209.                     case 'Z':
  210.                         zoomout(width/2,height/2);
  211.                         ReplyMsg((struct Message *)msg);
  212.                         longjmp(jb,1);
  213.                         exit(EXIT_FAILURE); /* should never reach here */
  214.                     }
  215.             }                
  216.         ReplyMsg((struct Message *)msg);
  217.         }
  218.     }
  219.  
  220.     
  221. void zoomin(WORD x,WORD y)    
  222.     {
  223.     double nx,ny;
  224.     double dx,dy;
  225.     maxcount+=25;
  226.     dx=(x2-x1);    
  227.     dy=(y2-y1);
  228.     
  229.     nx=(dx*x)/width+x1;
  230.     ny=(dy*(height-y))/height+y1;
  231.     x1=nx-(dx/2)/2;
  232.     x2=nx+(dx/2)/2;
  233.     y1=ny-(dy/2)/2;
  234.     y2=ny+(dy/2)/2;
  235.     }
  236.     
  237.     
  238. void zoomout(WORD x,WORD y)
  239.     {
  240.     double nx,ny;
  241.     double dx,dy;
  242.     maxcount-=25;
  243.     if(maxcount<10) maxcount=10;
  244.     dx=(x2-x1);    
  245.     dy=(y2-y1);
  246.     
  247.     nx=(dx*x)/width+x1;
  248.     ny=(dy*(height-y))/height+y1;
  249.     x1=nx-(dx/2)*2;
  250.     x2=nx+(dx/2)*2;
  251.     y1=ny-(dy/2)*2;
  252.     y2=ny+(dy/2)*2;    
  253.     }
  254.  
  255. void drawmandel(void)
  256.     {
  257.     int i,j,j1;
  258.     double a,b,am,bm;
  259.     double u,v,w,x,y;
  260.  
  261.     int k;
  262.  
  263.     am=(x2-x1)/width;
  264.     bm=(y1-y2)/height;
  265.     
  266.     for(i=0,a=x1;i<width;i++)    
  267.         {
  268.         for(j=0,b=y2;j<height;)
  269.             {
  270.             HandleMsg();
  271.             for(j1=0;j1<8 && j<height;j1++,j++)
  272.                 {
  273.                 x=a;y=b;
  274.                 
  275.                 for(k=0;k<maxcount;k++)
  276.                     {
  277.                     u=x*x;
  278.                     v=y*y;
  279.                     if(u+v>16) break;
  280.                     w=2*x*y;
  281.                     x=u-v+a;
  282.                     y=w+b;
  283.                     }
  284.                 if(k==maxcount)
  285.                     {
  286.                     SetAPen(rp,1);
  287.                     }
  288.                 else
  289.                     {
  290.                     SetAPen(rp,k % (1<<depth));
  291.                     }
  292.                 WritePixel(rp,i,j);
  293.                 b+=bm;
  294.                 }
  295.             }
  296.         a+=am;
  297.         }
  298.     }
  299.  
  300.  
  301.